<template>
    <rx-layout>
        <div slot="center" style>
            <rx-fit>
                <div slot="toolheader" foldheader="true" foldbtn="false" border="false">
                    <div class="table-operator">
                        <a-button @click="addRole">{{i18n('list.addRoleBtn','新增角色')}}</a-button>
                    </div>
                    <span class="search-btn-box">
                <span class="search-btn" @click="searchshow"><i class="iconfont iconguolv"></i>{{$ti18('rxList.leachBtn','过滤')}}</span>
           </span>
                </div>
                <div slot="searchbar" btnalign="right"  v-if="fitSearch" @search="search" @cancel="cancel" @close="close">
                    <a-form layout="vertical">
                        <a-form-item :label="i18n('form.roleName','角色名称')">
                            <a-input :placeholder="i18n('form.roleName','角色名称','input')" v-model="queryParam.Q_TO_AUTH_NAME__S_LK"/>
                        </a-form-item>
                    </a-form>
                </div>
                <rx-grid
                    ref="table"
                    :allowRowSelect="true"
                    :multiSelect="true"
                    :columns="columns"
                    :defaultPageSize="10"
                    :url="'/api-bpm/bpm/core/bpmAuth/queryByProcess?authType=ROLE&defKey='+$route.params.defKey"
                    :queryParam="queryParam"
                    data-field="result.data"
                    :pageSizeOptions="['10','30','40']"
                    id-field="id"
                    @selectChange="onSelectChange"
                    @getListData="getListData"
                    :handRowClass="handRowClass"
                >
                    <span slot="activeTime" slot-scope="{ text, record }">
                        {{text=='longTime'?i18n('form.longTime','长期'):i18n('form.shortTime','短期')}}
                    </span>
                    <span slot="status" slot-scope="{ text, record }">
                        {{text=='YES'?i18n('form.valid','有效'):i18n('form.invalid','无效')}}
                    </span>
                    <span slot="action" slot-scope="{ text, record }" class="actionIcons" size="3">
                        <span v-if="record.status=='YES'" @click="setActiveTime(record.id)">{{i18n('list.setActiveTime','设置有效期')}}</span>
                        <span v-if="record.status=='YES'" @click="delAuth([record.id])">{{$ti18('rxList.delBtn','删除')}}</span>
                    </span>
                </rx-grid>
            </rx-fit>
        </div>
    </rx-layout>
</template>

<script>
    import BpmAuthApi from '@/api/bpm/core/bpmAuth'
    import {BaseList, RxFit, Dialog, RxUserInfo, Util} from "@lowcode/jpaas-common-lib";
    import BpmAuthSetActiveTime from "../BpmAuthSetActiveTime";
    import OsUserApi from "@/api/user/org/osUser";

    export default {
        name: 'BpmAuthRoleListByProcess',
        mixins: [BaseList],
        components: {
            RxFit,
            RxUserInfo
        },
        data() {
            return {
                columns: [
                    {
                        title: this.$ti18('rxList.index','序号'),
                        type: 'indexColumn',
                        width: 20,
                        dataIndex: 'serial',
                        scopedSlots: {customRender: 'serial'}
                    },
                    {title: this.i18n('form.roleName','用户名'), dataIndex: 'toAuthName', width: 30},
                    {title: this.i18n('form.activeTime','有效期'), dataIndex: 'activeTime', width: 50, scopedSlots: {customRender: 'activeTime'}},
                    {title: this.i18n('form.available','是否有效'), dataIndex: 'status', width:50, scopedSlots: {customRender: 'status'}},
                    {title: this.i18n('form.authorizer','授权人'), dataIndex: 'authUserName', width: 30},
                    {title: this.i18n('form.authTime','授权时间'), dataIndex: 'createTime', width: 50},
                    {title: this.i18n('form.deleter','删除人'), dataIndex: 'delUserName', width: 30},
                    {title: this.i18n('form.deleteTime','删除时间'), dataIndex: 'delTime', width: 50},
                    {title: this.$ti18('rxList.action','操作'), width: 100, dataIndex: 'action', scopedSlots: {customRender: 'action'}}
                ],
                comment: this.i18n('list.comment','流程授权表'),
                widthHeight: ['800px', '600px'],
                fitSearch: false,
            }
        },
        methods: {
            i18n(name,text,key){
                return this.$ti18(name,text,"BpmAuth",key);
            },
            delById(ids) {
                return BpmAuthApi.delAuth({ids: ids.join(',')});
            },
            getListByParams(parameter) {
                return BpmAuthApi.query(parameter)
            },

            delAuth(ids){
                let self_ = this;
                this.$confirm({
                    title: this.$ti18('rxMsa.actionTips','操作提示'),
                    zIndex: 20000,
                    content: self_.i18n('msa.delInfo','该操作将会删除该流程和按钮授权，是否确定执行该操作？'),
                    okText: this.$ti18('rxForm.ackBtn','确认'),
                    cancelText: this.$ti18('rxForm.cancelBtn','取消'),
                    onOk() {
                        self_.delById(ids).then(res => {
                            if (res.success && res.code == 200) {
                                self_.refreshGrid()
                            }
                            self_.selectedRowKeys = [];
                            self_.selectedRows = [];
                        })
                    },
                    onCancel() {
                    }
                })
            },
            addRole() {
                var config = {
                    curVm: this,
                    title: '选择角色',
                    data: {single: false, dimId: '2', showDimId: false},
                    widthHeight: ['1000px', '600px']
                }
                Dialog.openGroupDialog(config,
                    function (self, data) {
                        if (data && data.length > 0) {
                            BpmAuthApi.saveAddRoleArray(self.$route.params.defKey, data.map(item => {
                                return item.groupId
                            }).join(',')).then(res => {
                                self.refreshGrid();
                                self.selectedRowKeys = [];
                                self.selectedRows = [];
                            })
                        }
                    })
            },
            setActiveTime(pkId){
                var self=this;
                var baseConf = {
                    "curVm": this,
                    component: BpmAuthSetActiveTime,
                    title: self.i18n('list.resetExpirationDate','重设有效期'),
                    widthHeight: ['600px', '200px'],
                    data: {
                        pkId:pkId
                    }
                };
                Util.open(baseConf, function (action) {
                    if(action!='ok')return;
                    self.refreshGrid();
                    self.selectedRowKeys = [];
                    self.selectedRows = [];
                })
            },
            getListData(data){
                //刷新组件
                this.$forceUpdate();
            },
            handRowClass(record,index){
                if(record.activeTime!='longTime' && new moment()>new moment(record.activeTime)){
                    return 'simpleGrayRow';
                }
                if(record.status=='NO'){
                    return 'grayRow';
                }
                return '';
            }
        }
    }
</script>
<style scoped>

</style>

